package com.hugang.juc.reference;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * 引用队列
 * 其他引用在被回收之前会被引用队列保存
 *
 * 结果：
 * java.lang.Object@677327b6
 * java.lang.Object@677327b6
 * null
 *
 * null
 * null
 * java.lang.ref.WeakReference@14ae5a5
 *
 * @author hg
 * @date 2020/8/26 16:20
 */
public class ReferenceQueueDemo {

    public static void main(String[] args) {
        Object o1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
        WeakReference<Object> weakReference = new WeakReference<>(o1,referenceQueue);

        System.out.println(o1);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println();

        o1 = null;
        System.gc();
        System.out.println(o1);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
    }
}
